package com.googlecode.continuity.sample.basic.sampleproject.src;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.googlecode.continuity.plugin.gwtext.client.CenteredDialogBox;
import com.googlecode.continuity.plugin.gwtext.client.message.GWTExtensionMessages;

/**
 * The ConfirmBox provides a simple confirm dialog which allows for the user to select a positive answer (upon which an
 * action is performed) or to answer negatively which doesn't result in any action.
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class ConfirmBox extends CenteredDialogBox {
  ClickListener callback;

  // message i18n
  private static final GWTExtensionMessages messages = (GWTExtensionMessages) GWT.create(GWTExtensionMessages.class);

  /**
   * Initializes a new confirm box with message and action to be performed upon positive answer, using default strings
   * for positive and negative action labels.
   *
   * @param question the question to be presented to the user
   * @param callback an action to be performed when the user selects the positive answer
   */
  public ConfirmBox(String question, ClickListener callback) {
    this(question, callback, messages.ok(), messages.cancel());
  }

  /**
   * Initializes a new confirm box with message and action to be performed upon positive answer. Also lets you define
   * the positive and negative action labels.
   *
   * @param question the question to be presented to the user
   * @param callback callback an action to be performed when the user selects the positive answer
   * @param positive positive action label
   * @param negative negative action label
   */
  public ConfirmBox(String question, ClickListener callback, String positive, String negative) {
    this.callback = callback;
    setText(messages.confirmBoxHeader());

    Button positiveButton = new Button(positive, callback);
    Button negativeButton = new Button(negative, finished);
    Label questionLabel = new Label(question);
    frame.insert(questionLabel, frame.getWidgetIndex(buttons));

    buttons.add(positiveButton);
    buttons.add(negativeButton);

    positiveButton.addClickListener(finished);

    positiveButton.setFocus(true);

    addStyleName("confirmBox");
    questionLabel.addStyleName("question");

    frame.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
  }
}